package jdbc2;

import com.tedu.InputUtil;

import java.sql.Connection;
import java.sql.Statement;

/*
* 完成用户注册的功能
* */
public class RegDemo {
    public static void main(String[] args) {
        UserInfo userInfo = InputUtil.getInputObject(new UserInfo(),"欢迎注册");
        System.out.println(userInfo);
        try (Connection conn = DBUtil.getConnection()) {
            Statement stmt = conn.createStatement();
            /*
            * 将接收的数据插入到数据库表中
            * INSERT INTO userinfo(username,password,nickname,age) VALUES('GGBond','123456','猪猪侠','18');
            * */
            String sql = "INSERT INTO userinfo(username,password,nickname,age)" +
                    " VALUES('"+ userInfo.getUsername()+"','"+ userInfo.getPassword()+"'," +
                    "'"+ userInfo.getNickname()+"','"+ userInfo.getAge()+"')";
            System.out.println(sql);
            int num = stmt.executeUpdate(sql);
            System.out.println(num > 0 ? "注册成功" : "注册失败");
            System.out.println(num);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
